<?php

namespace app\controller;
use think\facade\Db;
use think\Request;

class Xhskeywords extends Common
{
	public function read(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhskeywords:read')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}

		$where = [];
		$limit = $request->param('limit/d', 20);
		$order = $request->param('order', 'desc', 'trim');
		$sort = $request->param('sort', 'status asc, id desc', 'trim');

		$bazaarId = $request->param('bazaarId', '', 'trim');
		if (empty($bazaarId) == false) {
				array_push($where, ['bazaarId', '=', $bazaarId]);
		}

		$keywords = $request->param('keywords', '', 'trim');
		if (empty($keywords) == false) {
				array_push($where, ['keywords', 'like', "%{$keywords}%"]);
		}

		$userStat = $request->param('userStat/d');
		if ($userStat === 0 || $userStat === 1 || $userStat === 2) {
				array_push($where, ['status', '=', $userStat]);
		}

		$paginate = Db::name('cfg_xhs_keywords')->field('`id`, `bazaarId`, `keywords`, `priority`, `status`, `deviceId`, FROM_UNIXTIME(createtime, \'%Y-%m-%d %H:%i:%s\') as createtime, FROM_UNIXTIME(datetime, \'%Y-%m-%d %H:%i:%s\') as datetime')
			->where($where)->order($sort, $order)->paginate($limit);

		$xhsBazaar = Db::table('cfg_xhs_bazaar')->field('id, name')->where('status', 1)->select()->toArray();
		$xhsDevicer = Db::table('cfg_xhs_device')->field('id, `deviceId`, `name`')->select()->toArray();
		return json(['code' => 0, 'msg' => '', 'count' => $paginate->total(), 'data' => $paginate->items(), 'bazaar' => $xhsBazaar, 'device' => $xhsDevicer]);
	}

	/**
	 * 创建数据
	 *
	 * @return
	 */
	public function create(Request $request) {
		if($this->hasPermission($request->sessInfo, 'xhskeywords:create')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$index = 0;
		$priority = $request->param('priority/d', '', 'trim');
		if (empty($priority)) {
				$priority = 0;
		}
		$bazaarId = $request->param('bazaarId', '', 'trim');
		$keywords = $request->param('keywords', '', 'trim');
		$array = explode("\n", $keywords);
		foreach ($array as $value) {
				if ($value) {
						$value = trim($value);
						$id = Db::table('cfg_xhs_keywords')->where('keywords', $value)->value('id');
						if (empty($id)) {
								$index++;
								Db::name('cfg_xhs_keywords')->insert(
										[
												'bazaarId' => $bazaarId,
												'priority' => $priority,
												'createtime' => time(),
												'keywords' => $value,
												'status' => 0,
										]
								);
						}
				}
		}
		return json(['code' => 0, 'msg' => '成功导入' . $index . '个关键词']);
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function delete(Request $request) {
		if($this->hasPermission($request->sessInfo, 'xhskeywords:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$param = $request->param();
		if( !isset($param['id']) ) {
			return ['code'=> 403, 'msg'=> '操作失败!'];
		}
		$index = 0;
		$array = explode(',', $param['id']);
		foreach ($array as $value) {
			$index++;
			Db::table('cfg_xhs_keywords')->where('id', $value)->delete();
		}
		return json(['code' => 0, 'msg' => '成功删除' . $index . '个关键词']);
	}

	/**
	 * 删除数据
	 *
	 * @return
	 */
	public function dels(Request $request)
	{
		if($this->hasPermission($request->sessInfo, 'xhskeywords:delete')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$index = 0;
		$keywords = $request->param('keywords', '', 'trim');
		$array = explode("\n", $keywords);
		foreach ($array as $value) {
				if ($value) {
						$value = trim($value);
						$id = Db::table('cfg_xhs_keywords')->where('keywords', $value)->value('id');
						if ($id) {
								$index++;
								Db::table('cfg_xhs_keywords')->where('id', $id)->delete();
						}
				}
		}
		return json(['code' => 0, 'msg' => '成功删除' . $index . '个关键词']);
	}

	/**
	 * 移动关键词行业
	 *
	 * @return
	 */
	public function movekeywords(Request $request) {
		if($this->hasPermission($request->sessInfo, 'xhskeywords:create')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$bazaarId = $request->param('bazaarId/d', '', 'trim');
		if (empty($bazaarId)) {
				return ['code'=> 403, 'msg'=> '关键词行业不能为空!'];;
		}
		$ids = $request->param('ids', '', 'trim');
		if (empty($ids)) {
				return ['code'=> 403, 'msg'=> '移动关键词ID不能为空!'];;
		}
		$index = Db::name('cfg_xhs_keywords')->where('id', 'in', $ids)->count();
		Db::name('cfg_xhs_keywords')->where('id', 'in', $ids)->data(['bazaarId' => $bazaarId])->update();
		return json(['code' => 0, 'msg' => '成功移动' . $index . '个关键词']);
	}
	
	/**
	 * 重置关键词
	 *
	 * @return
	 */
	public function resetkeywords(Request $request) {
		if($this->hasPermission($request->sessInfo, 'xhskeywords:create')) {
			return ['code'=> 403, 'msg'=> '你没有权限执行此操作!'];;
		}
		$ids = $request->param('id', '', 'trim');
		if (empty($ids)) {
				return ['code'=> 403, 'msg'=> '移动关键词ID不能为空!'];;
		}
		$index = Db::name('cfg_xhs_keywords')->where('id', 'in', $ids)->count();
		Db::name('cfg_xhs_keywords')->where('id', 'in', $ids)->data(['status' => 0])->update();
		return json(['code' => 0, 'msg' => '成功重置' . $index . '个关键词']);
	}

}
